import {View, Input} from "@tarojs/components";
import "./homeSelect.less"
import React, {useEffect, useState} from 'react'
import Taro from "@tarojs/taro"
import {AtInput, AtForm} from 'taro-ui'
import http from "../../../service/api";
import store from "../../../store";

function Select() {

    const [data, setData] = useState("")
    const [hot,setHot]=useState(store.hotGoods)
    const [history,setHistory]=useState(store.historyGoods)
    const change = (e) => {
        console.log(e)
        setData(e)
    }
    const searchClick = () => {
    //返回首页,携带数据data，返回首页后重新请求
    //    将data作为数据发送请求添加到历史记录
        Taro.request({
            url: http.api4 + "/portalIndex/queryGoods",
            method: "get",
            header: {
                'content-type': 'application/x-www-form-urlencoded' // 默认值
            },
            data: {
                uid: store.userInformation.id,
                name:data
            },
            success(res) {
                // console.log("hot")
                console.log(res)
                // console.log(res.data.information)
                store.setSelectedData(res.data.data)
                setData("")
                Taro.navigateTo({
                    url: "../homeSelected/homeSelected"
                });
            },
            fail(err) {
                console.log("失败了")
            }
        })
    }
    return (
        <View className="select">
            <View className="what">今天想找什么？</View>
            <AtInput
                id="input"
                name='value1'
                type='text'
                autoFocus="ture"
                placeholder='写上你喜爱的商品，我来帮你搜'
                value={data}
                onChange={change}
            >
                <View className='at-icon at-icon-search' onClick={searchClick}/>
            </AtInput>
            <View className="hotSearch">
                <View>热门搜索</View>
                <View className="hotName">
                    {hot.map((item)=>{
                        return(
                            <View className="hotname" onClick={()=>{
                                setData(item)
                            }}>
                                {item}
                            </View>
                        )
                    })}
                </View>
            </View>
            <View className="history">
                <View>历史搜索</View>
                <View className="historyName">
                    {history.map((item)=>{
                        return(
                            <View className="hotname" onClick={()=>{
                                setData(item)
                            }}>
                                {item}
                            </View>
                        )
                    })}
                </View>
            </View>
        </View>
    )
}

export default Select